package com.example.myapplication.rxjava;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.myapplication.R;
import com.example.myapplication.databinding.RxviewBinding;
import com.jakewharton.rxbinding4.view.RxView;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import kotlin.Unit;

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/8/22$ 4:54 PM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/8/22$ 4:54 PM$
 * 修改备注:  [说明本次修改内容]
 */
public class RxviewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RxviewBinding binding = DataBindingUtil.setContentView(this, R.layout.rxview);
//        test1(binding);
        test2(binding);
    }

    private void test2(RxviewBinding binding) {
        //钩子函数
        RxJavaPlugins.setOnObservableAssembly(new io.reactivex.functions.Function<Observable, Observable>() {
            @Override
            public Observable apply(@NonNull Observable observable) throws Exception {
                Logger.d("RxviewActivity===apply: "+observable);
                return null;
            }
        });
        Observable.just(111).subscribe();

    }

    private void test1(com.example.myapplication.databinding.RxviewBinding binding) {




        RxView.clicks(binding.button2).throttleFirst(20,
                TimeUnit.SECONDS).map(new Function<Object, Boolean>() {
            @Override
            public Boolean apply(Object o) throws Throwable {
                return null;
            }
        }).doOnNext(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean o) throws Throwable {
                binding.button2.setEnabled(o);
//20秒之内只能发送一次事件，过了20秒之后重新设置true
            }
        });
    }
}
